package database;

import org.junit.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class TestMain {
    private EmpDao empDao = new EmpDaolmpl();  // 定义变量，是指可以调用EmpDaolmpl中的函数

    // 连接测试
    @Test
    public void testConnection(){
        try {
            Connection connection = JDBCDemo.getConnection();
            System.out.println("connection = " + connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 测试查询数据
    @Test
    public void testQueryData(){
        Connection connection = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            connection = JDBCDemo.getConnection();
            String sql = "select * from emp";
            pstmt = connection.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while(rs.next()){
                System.out.println(rs.getInt(1)+","+rs.getString(2)+","+rs.getString(3)+","+rs.getDouble(4)+","+rs.getInt(5));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCDemo.closeAll(rs, pstmt, connection);
        }
    }

    // 测试添加数据
    @Test
    public void testAddEmp(){
        Emp emp = new Emp();
        emp.setEmpno(1005);
        emp.setEname("罗琦");
        emp.setJob("运维");
        emp.setSal(3400);
        emp.setDeptno(10);
        empDao.addEmp(emp);
    }

    // 测试查询数据
    @Test
    public void testFindEmp(){
        Emp emp = empDao.findEmpByEmpno(1002);
        System.out.println("emp = " + emp);
    }

    // 测试统计数据
    @Test
    public void testCount(){
        int i = empDao.totalCount();
        System.out.println("i = " + i);
    }

    // 测试删除数据
    @Test
    public void testDelEmp(){
        empDao.delEmpByEmpno(1005);
    }

    // 测试模糊查询
    @Test
    public void testLikeQueryEmps(){
        List<Emp> empList = empDao.likeQueryEmps("运维");
        for (Emp emp : empList) {
            System.out.println("emp = " + emp);
        }
    }
}
